package za.co.chris.traintimes;

import java.util.Calendar;
import java.util.List;

import za.co.chris.traintimes.beans.Constants;
import za.co.chris.traintimes.beans.Line;
import za.co.chris.traintimes.beans.Station;
import za.co.chris.traintimes.database.DatabaseHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.NumberPicker;
import android.widget.NumberPicker.Formatter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimeSelect extends Activity {

	DatabaseHelper helper;
	Spinner fromStationSpinner;
	Spinner toStationSpinner;
	Spinner fromLineSpinner;
	Spinner toLineSpinner;
	Spinner interchangeSpinner;
	TextView interchangeLable;
	TimePicker timePicker;
	NumberPicker dayOfWeekPicker;
	private Station fromStation;
	private Station toStation;
	private Station interchange;
	private Line fromLine;
	private Line toLine;
	NumberPIckerFormatter formatter = new NumberPIckerFormatter();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_time_select);

		helper = DatabaseHelper.getInstance(this);
		helper.open();
		List<Line> lines = helper.getLines();

		ArrayAdapter<Line> lineAdapter = new ArrayAdapter<Line>(this, android.R.layout.simple_spinner_item, lines);
		fromLineSpinner = ((Spinner) findViewById(R.id.fromLine));
		toLineSpinner = ((Spinner) findViewById(R.id.toLine));
		interchangeSpinner = (Spinner) findViewById(R.id.interchange);
		interchangeLable = (TextView) findViewById(R.id.intercangeLable);
		fromLineSpinner.setAdapter(lineAdapter);
		toLineSpinner.setAdapter(lineAdapter);
		fromStationSpinner = (Spinner) findViewById(R.id.fromStation);
		toStationSpinner = (Spinner) findViewById(R.id.toStation);
		timePicker = (TimePicker) findViewById(R.id.timePicker1);
		dayOfWeekPicker = (NumberPicker) findViewById(R.id.dayOfWeek);
		dayOfWeekPicker.setFormatter(formatter);
		dayOfWeekPicker.setMinValue(1);
		dayOfWeekPicker.setMaxValue(7);
		dayOfWeekPicker.setValue(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
		dayOfWeekPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		fromLineSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
				fromLine = (Line) ((Spinner) parent).getSelectedItem();
				populateStations(fromStationSpinner, fromLine);
				polulateInterchange(fromLine, toLine);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		toLineSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1, int arg2, long arg3) {
				toLine = (Line) ((Spinner) parent).getSelectedItem();
				populateStations(toStationSpinner, toLine);
				polulateInterchange(fromLine, toLine);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void populateStations(Spinner spinner, Line line) {
		List<Station> stations = helper.getStationsOnLine(line.getName());
		ArrayAdapter<Station> adapter = new ArrayAdapter<Station>(this, android.R.layout.simple_spinner_item, stations);
		spinner.setAdapter(adapter);
	}

	private void polulateInterchange(Line fromLine, Line toLine) {
		if (fromLine == null) {
			fromLine = (Line) fromLineSpinner.getItemAtPosition(0);
		}
		if (toLine == null) {
			toLine = (Line) toLineSpinner.getItemAtPosition(0);
		}

		if (!fromLine.getName().equals(toLine.getName())) {
			interchangeSpinner.setVisibility(View.VISIBLE);
			interchangeLable.setVisibility(View.VISIBLE);
			List<Station> stations = helper.getInterchangeList(fromLine.getName(), toLine.getName());
			ArrayAdapter<Station> stationAdapter = new ArrayAdapter<Station>(TimeSelect.this, android.R.layout.simple_spinner_item, stations);
			interchangeSpinner.setAdapter(stationAdapter);
		} else {
			interchangeSpinner.setVisibility(View.INVISIBLE);
			interchangeLable.setVisibility(View.INVISIBLE);
			interchange = null;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	public void findRoute(View view) {
		fromStation = (Station) fromStationSpinner.getSelectedItem();
		int fromPosition = fromLineSpinner.getSelectedItemPosition();
		toStation = (Station) toStationSpinner.getSelectedItem();
		int toPosition = toStationSpinner.getSelectedItemPosition();
		if (interchangeSpinner.getVisibility() == View.VISIBLE) {
			interchange = (Station) interchangeSpinner.getSelectedItem();
		}
		Intent intent = new Intent(this, TimeResults.class);
		intent.putExtra(Constants.FROM_STATION_KEY, fromStation.getStationName());
		intent.putExtra(Constants.FROM_STATION_POSITION_KEY, fromPosition);
		intent.putExtra(Constants.TO_STATION_KEY, toStation.getStationName());
		intent.putExtra(Constants.TO_STATION_POSITION_KEY, toPosition);
		intent.putExtra(Constants.FROM_LINE_KEY, fromLine.getName());
		intent.putExtra(Constants.TO_LINE_KEY, toLine.getName());
		if (interchange != null) {
			intent.putExtra(Constants.INTERCHANGE_STATION_KEY, interchange.getStationName());
		}
		intent.putExtra(Constants.SELECTED_TIME_KEY, timePicker.getCurrentHour() + ":" + timePicker.getCurrentMinute());
		intent.putExtra(Constants.DAY_SELECTED_KEY, formatter.format(dayOfWeekPicker.getValue()));
		startActivity(intent);
	}
}

class NumberPIckerFormatter implements Formatter {

	@Override
	public String format(int value) {
		switch (value) {
		case 1:
			return "SUN";
		case 2:
			return "MON";
		case 3:
			return "TUE";
		case 4:
			return "WED";
		case 5:
			return "THU";
		case 6:
			return "FRI";
		case 7:
			return "SAT";

		}
		return "Unk";
	}
}
